Examples of plotly
library(tidyverse)
library(p8105.datasets)
library(plotly)
library(flexdashboard)
data("instacart")
Clean the dataset
instacart_clean =
instacart |>
select(
user_id,
product_name,
department,
aisle,
add_to_cart_order,
reordered,
order_hour_of_day,
order_dow
) |> drop_na()
Orders by Department (bar plot)
dept_counts = instacart_clean |>
count(department) |>
arrange(desc(n))
plot_ly(
data = dept_counts,
x = ~reorder(department, n),
y = ~n,
type = "bar",
color = ~department,
colors = "viridis") |>
layout(
title = "Number of Orders by Department",
xaxis = list(title = "Department"),
yaxis = list(title = "Number of Orders"))
Distribution of Add-to-Cart Order by Department (box plot)
plot_ly(
data = instacart_clean,
x = ~department,
y = ~add_to_cart_order,
color = ~department,
type = "box",
colors = "viridis") |>
layout(
title = "Add-to-Cart Order by Department",
xaxis = list(title = "Department"),
yaxis = list(title = "Add-to-Cart Position"))